package wiki.xsx.jg.core;

import java.math.BigDecimal;
import java.util.*;

/**
 * PostgreSQL实现类
 */
public class PostgreSQLService extends AbstractService{

    public PostgreSQLService(Database database) {
        super(database);
    }

    @Override
    public String getDropTableSQL(String className) {
        StringBuilder sql = new StringBuilder();
        sql.append("DROP TABLE IF EXISTS ").append(className);
        return sql.toString();
    }


    @Override
    public Class<?> getTypeClass(String dataTypeName, int length, int scale) {
        if (dataTypeName.matches("CHAR|VARCHAR|TEXT")){
            return String.class;
        }else if(dataTypeName.matches("INT4|SERIAL4|INTERVAL")) {
            return Integer.class;
        }else if(dataTypeName.matches("FLOAT4")) {
            return Float.class;
        }else if(dataTypeName.matches("FLOAT8|MONEY")) {
            return Double.class;
        }else if(dataTypeName.matches("DECIMAL")) {
            return BigDecimal.class;
        }else if(dataTypeName.matches("INT2|SERIAL2")) {
            return Short.class;
        }else if(dataTypeName.matches("INT8|SERIAL8")) {
            return Long.class;
        }else if(dataTypeName.matches("DATE|TIME|TIMETZ|TIMESTAMP|TIMESTAMPTZ")) {
            return Date.class;
        }else if(dataTypeName.matches("BOOL")) {
            return Boolean.class;
        }else{
            return Object.class;
        }
    }

    @Override
    public String getDataType(Class<?> type) {
        StringBuilder builder = new StringBuilder();
        switch (type.getSimpleName().toLowerCase()){
            case "string":
                builder.append("VARCHAR(255)");
                break;
            case "integer":
                builder.append("INT4");
                break;
            case "float":
                builder.append("FLOAT4");
                break;
            case "double":
                builder.append("FLOAT8");
                break;
            case "bigdecimal":
                builder.append("DECIMAL");
                break;
            case "short":
                builder.append("INT2");
                break;
            case "long":
                builder.append("INT8");
                break;
            case "date":
                builder.append("DATE");
                break;
            case "timestamp":
                builder.append("TIMESTAMP");
                break;
            case "time":
                builder.append("TIME");
                break;
            case "boolean":
                builder.append("BOOL");
                break;
            default:
                builder.append("VARCHAR(255)");
        }
        return builder.toString();
    }
}
